<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :inline="true" :model="listQuery" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="">
              <el-input
                v-model="listQuery.name"
                placeholder="请输入姓名"
                clearable
                @keyup.enter.native="handleFilter"
              >
              </el-input>
            </el-form-item>
            <el-form-item label="">
              <el-cascader
                v-model="selectedKeys"
                :options="accountList"
                :props="props"
                @change="handleChange"
                clearable
                placeholder="科目"
              ></el-cascader>
            </el-form-item>
            <el-form-item label="">
              <el-date-picker
                v-model="listQuery.search_date"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click.native="handleFilter"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="">
              <el-select
                v-model="listQuery.account_type"
                placeholder="银行账号"
                clearable
              >
                <el-option
                  v-for="item in banks"
                  :label="item.account"
                  :value="item.id"
                  :key="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-select
                v-model="listQuery.order_type"
                placeholder="单据类型"
                clearable
              >
                <el-option label="收入" value="0"></el-option>
                <el-option label="支出" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input
                v-model="listQuery.order_no"
                placeholder="单据号"
                clearable
                @keyup.enter.native="handleFilter"
              >
              </el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                v-model="listQuery.desc"
                placeholder="备注"
                clearable
                @keyup.enter.native="handleFilter"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        :data="journals"
        border
        v-loading="listLoading"
        fit
        size="mini"
        highlight-current-row
        show-summary
        :summary-method="getSummaries"
      >
        <el-table-column
          type="index"
          label="#"
          align="center"
        ></el-table-column>
        <el-table-column
          v-for="item in tableColumns"
          :label="item.title"
          :prop="item.key"
          :key="item.key"
          :width="item.width"
          :align="item.align ? item.align : 'center'"
          :formatter="dataFormatter"
        ></el-table-column>

        <el-table-column
          align="center"
          label="操作"
          width="100px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getJournals"
      />
    </el-card>
    <!-- 对话框表单 -->
    <el-dialog :visible.sync="dialogVisible" :title="getTitle" width="800px">
      <el-form
        :model="form"
        ref="formRef"
        label-width="100px"
        label-position="right"
        style="width: 95%; padding: 0 30px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="单据号" prop="order_no">
              <el-input
                type="text"
                v-model="form.order_no"
                placeholder="单据号"
                maxlength="24"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开单时间" prop="order_time">
              <el-date-picker
                v-model="form.order_time"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="选择开单时间"
                readonly
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="form.name" placeholder="案件名称" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经手人" prop="user_name">
              <el-input
                v-model="form.user_name"
                placeholder="经手人"
                readonly
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="会计科目" prop="account_name">
              <el-input
                v-model="form.account_name"
                placeholder="会计科目"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款账号" prop="account_type_name">
              <el-input
                v-model="form.account_type_name"
                placeholder="收款账号"
                readonly
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="order_type_name">
              <el-input
                v-model="form.order_type_name"
                placeholder="类型"
                readonly
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作类型" prop="oper_type">
              <el-input
                v-model="form.oper_type"
                placeholder="操作类型"
                readonly
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收入金额" prop="income_money">
              <el-input
                v-model="form.income_money"
                placeholder="收入金额"
                readonly
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支出金额" prop="expense_money">
              <el-input
                v-model="form.expense_money"
                placeholder="支出金额"
                readonly
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="总金额" prop="total_money">
              <el-input
                v-model="form.total_money"
                placeholder="总金额"
                readonly
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="oper_time">
              <el-input
                v-model="form.oper_time"
                placeholder="创建时间"
                readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input v-model="form.desc" placeholder="备注" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="danger" @click="dialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getParentsPath } from "@/utils/index";
import { getCurrentDate } from "@/utils/datetime";
import { getJournalList, getJournalById } from "@/api/sale";
import { getBanks } from "@/api/bank";
import { get_account_titles } from "@/api/account";
import Pagination from "@/components/Pagination";

export default {
  name: "Journal",

  data() {
    return {
      form: {},
      title: "流水账",
      journals: [],
      accountList: [],
      banks: [],
      selectedKeys: [],
      dialogVisible: false,
      dialogType: "new",
      receiptList: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20,
        name: null,
        search_date: "",
        account_title: null,
        account_type: null,
        order_no: null,
        order_type: null,
        desc: null,
      },
      tableColumns: [
        {
          key: "order_no",
          title: "单据号",
          width: "140",
        },
        {
          key: "order_time",
          title: "单据日期",
        },
        {
          key: "name",
          title: "用户",
        },
        {
          key: "account_type_name",
          title: "银行名称",
        },

        {
          key: "account_name",
          title: "会计科目",
          width: "160px"
        },

        {
          key: "order_type_name",
          title: "类型",
        },
        {
          key: "oper_type",
          title: "操作类型",
        },
        {
          key: "total_money",
          title: "总金额(元)",
        },
        {
          key: "income_money",
          title: "收入金额",
        },
        {
          key: "expense_money",
          title: "支出金额",
        },
        {
          key: "desc",
          title: "备注",
        },
        {
          key: "user_name",
          title: "经手人",
        },
      ],

      props: {
        expandTrigger: "hover",
        value: "id",
        label: "name",
        children: "children",
      },

      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["uid"]),
    getTitle() {
      return this.dialogType == "new"
        ? `新增${this.title}`
        : `${this.title}详情`;
    },
  },
  components: {
    Pagination,
  },
  created() {
    this.getJournals();
    this.getBankList();
    this.getAccounts();
  },
  methods: {
    // 流水账列表
    getJournals() {
      this.listLoading = true;
      getJournalList(this.listQuery)
        .then((res) => {
          this.total = res.count;
          this.journals = res.results;
          this.listLoading = false;
        })
        .catch((error) => {
          console.log(error.message);
          this.$message.error("获取数据失败");
          this.listLoading = false;
        });
    },

    getBankList() {
      getBanks().then((res) => {
        this.banks = res;
        let index = this.banks.findIndex((item) => item.id == 1);
        if (index > -1) {
          this.banks.splice(index, 1);
        }
      });
    },

    getAccounts() {
      get_account_titles({}).then((res) => {
        this.accountList = res;
      });
    },

    handleChange(value) {
      console.log("selectedKeys:", this.selectedKeys);
      if (this.selectedKeys.length > 0) {
        this.listQuery.account_title = this.selectedKeys[
          this.selectedKeys.length - 1
        ];
      } else {
        this.listQuery.account_title = undefined;
      }
    },

    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "合计";
          return;
        }
        if (
          column.property === "income_money" ||
          column.property === "expense_money"
        ) {
          const values = data.map((item) => item[column.property]);
          sums[index] = values
            .reduce((prev, curr) => {
              return prev + curr;
            }, 0)
            .toFixed(2);
          sums[index] += " 元";
        } else {
          return;
        }
      });
      return sums;
    },
    dataFormatter(row, column, cellValue, index) {
      if (column.property == "total_money") {
        return row.total_money.toFixed(2);
      } else if (column.property == "income_money") {
        return row.income_money.toFixed(2);
      } else if (column.property == "expense_money") {
        return row.expense_money.toFixed(2);
      } else {
        return cellValue;
      }
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1;
      this.getJournals();
    },

    // 查看详情
    handleEdit(scope) {
      this.form = Object.assign({}, scope.row);
      this.dialogType = "edit";
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang='scss' scoped>
.app-container {
  // .box-card{
  //   margin-top: 30px;
  // }
  .el-table {
    margin-top: 10px;
    width: 100%;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
  .el-select {
    width: 100%;
  }
  .item {
    margin-top: 8px;
  }
  .el-cascader{
    width: 280px!important
  }
}
</style>